package com.shenyong.controller;

import com.alibaba.fastjson.JSONObject;
import com.shenyong.entity.LoginUsers;
import com.shenyong.entity.Users;
import com.shenyong.exception.MyException;
import com.shenyong.ladmin.logConfig.SysLogAspects;
import com.shenyong.util.JwtUtil;
import com.shenyong.util.MD5Untils;
import lombok.extern.slf4j.Slf4j;
import com.shenyong.service.LevelService;
import com.shenyong.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.session.mgt.WebSessionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author
 * @site
 * @company
 * @create
 */
@Slf4j
@RestController
@Api("第一个API测试")
public class LoginController {
    @Autowired
    private BaseController baseController;
    @Autowired
    private UserService userService;
    @Autowired
    private LevelService levelService;
      @ApiOperation(value = "登录", notes = "登录方法", produces = "application/json")
      @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", required = false, dataType = "String")
              , @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "String")})
      @RequestMapping(value = "/login")
      public Map<String, Object> login(Users users, HttpServletResponse httpServletResponse) {
          System.out.println(users+"users1");
          Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
          log.warn("执行登录操作!");
          LoginUsers use=userService.getUsers(users);
          baseController.setToken(use.getToken());
          try {
              if(use!=null){
                  httpServletResponse.setHeader("Authorization",use.getToken());
                  httpServletResponse.setHeader("Access-Control-Expose-Headers", "Authorization");
              }
              resultMap.put("loginUser",use);
              System.out.println(resultMap);
    }catch (MyException e) {
        resultMap.put("msg",e.getMessage());
    }catch (LockedAccountException e) {
        resultMap.put("msg",e.getMessage());
    }catch (IncorrectCredentialsException e) {
        resultMap.put("msg",e.getMessage());
    }catch (AccountException e) {
        resultMap.put("msg",e.getMessage());
    }
/*
          LoginUsers loginUsers=userService.getName(username1);
          Subject subject = SecurityUtils.getSubject();
          if (subject.isAuthenticated()) {
              throw new UnknownAccountException("用户已登录");
          }*/
/*
       UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        token.setRememberMe(true);
        subject.login(token);
        Serializable SessionId = subject.getSession().getId();
        System.out.println(SessionId+"sss"+"SessionId");
        JSONObject json=new JSONObject();
        json.put("token",SessionId);*/
        return resultMap;
    }
    @RequestMapping("/updatePasword")
    public int updatePasword(Users users){
        String nickName1= baseController.getToken();
        String nickName= JwtUtil.getUsername(nickName1);
        LoginUsers user= userService.getName(nickName);
        int userid=user.getUser().getUser_id();
        if (!MD5Untils.verify(users.getUser_nickname(),user.getUser().getUser_password())) {
            throw new AccountException("原密码错误，请仔细一点哦(`･ω･′)ゞ");
        }
        users.setUser_id(userid);
        String password= MD5Untils.generate(users.getUser_password());
        users.setUser_password(password);
        return userService.updatePasword(users);
    }
    @RequestMapping(value = "/unauth")
    @ResponseBody
    public Object unauth() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "1000000");
        map.put("msg", "未登录");
        return map;
    }
}
